﻿// (c) Comkarl Calipso
// By Matías Fidemraizer (http://www.matiasfidemraizer.com)
// -------------------------------------------------
// http://calipso.codeplex.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Comkarl.Web.Configuration;
using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;

namespace Comkarl.Web
{
    /// <summary>
    /// Represents a set of run-time variables used across ASP.NET Calipso infrastructure.
    /// </summary>
    public static class RuntimeEnvironment
    {
        static RuntimeEnvironment()
        {
            _calipsoSection = new Lazy<CalipsoSection>
            (
                () =>
                {
                    if (!string.IsNullOrEmpty(HostingEnvironment.ApplicationVirtualPath))
                    {
                        return (CalipsoSection)ConfigurationManager.GetSection("calipso");
                    }
                    else
                    {
                        ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                        map.ExeConfigFilename = FullPhysicalApplicationPath + @"\Web.config";

                        return (CalipsoSection)ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).GetSection("calipso");
                    }
                }
            );
        }

        private static readonly Lazy<CalipsoSection> _calipsoSection;

        private static CalipsoSection CalipsoSection
        {
            get
            {
                return _calipsoSection.Value;
            }
        }

        /// <summary>
        /// Gets current ASP.NET application full physical path.
        /// </summary>
        public static string FullPhysicalApplicationPath
        {
            get
            {
                if (!string.IsNullOrEmpty(HostingEnvironment.ApplicationVirtualPath))
                {
                    return HostingEnvironment.ApplicationPhysicalPath;
                }
                else
                {
                    return Environment.CurrentDirectory;
                }
            }
        }

        /// <summary>
        /// Gets CSS cache virtual path.
        /// </summary>
        public static string CssCacheVirtualPath
        {
            get
            {
                return VirtualPathUtility.ToAppRelative(CalipsoSection.CssManager.CachePath, HostingEnvironment.ApplicationVirtualPath); 
            }
        }

        /// <summary>
        /// Gets CSS cache relative path.
        /// </summary>
        public static string CssCacheRelativePath
        {
            get
            {
                string path = VirtualPathUtility.ToAbsolute(CssCacheVirtualPath, HostingEnvironment.ApplicationVirtualPath);

                if (path.Last() != '/')
                {
                    path += '/';
                }

                return path;
            }
        }

        /// <summary>
        /// Gets CSS cache absolute path.
        /// </summary>
        public static string CssCacheAbsolutePath
        {
            get
            {
                return Path.Combine(HostingEnvironment.ApplicationPhysicalPath, CalipsoSection.CssManager.CachePath.Replace("~", string.Empty).Replace(@"\", string.Empty));
            }
        }

        /// <summary>
        /// Gets JavaScript localization resource virtual path.
        /// </summary>
        public static string JsResourceVirtualPath
        {
            get
            {
                return VirtualPathUtility.ToAppRelative(CalipsoSection.JavaScriptManager.ResourcePath, HostingEnvironment.ApplicationVirtualPath);
            }
        }

        /// <summary>
        /// Gets JavaScript localization resource relative path.
        /// </summary>
        public static string JsResourceRelativePath
        {
            get
            {
                string path = VirtualPathUtility.ToAbsolute(JsResourceVirtualPath, HostingEnvironment.ApplicationVirtualPath);

                if (path.Last() != '/')
                {
                    path += '/';
                }

                return path;
            }
        }

        /// <summary>
        /// Gets JavaScript localization resource absolute path.
        /// </summary>
        public static string JsResourceAbsolutePath
        {
            get
            {
                return Path.Combine(FullPhysicalApplicationPath, CalipsoSection.JavaScriptManager.ResourcePath.Replace("~", string.Empty).Replace(@"\", string.Empty));
            }
        }

        /// <summary>
        /// Gets JavaScript cache virtual path.
        /// </summary>
        public static string JsCacheVirtualPath
        {
            get
            {
                return VirtualPathUtility.ToAppRelative(CalipsoSection.JavaScriptManager.CachePath, HostingEnvironment.ApplicationVirtualPath); 
            }
        }

        /// <summary>
        /// Gets JavaScript cache relative path.
        /// </summary>
        public static string JsCacheRelativePath
        {
            get
            {
                string path = VirtualPathUtility.ToAbsolute(JsCacheVirtualPath, HostingEnvironment.ApplicationVirtualPath);

                if (path.Last() != '/')
                {
                    path += '/';
                }

                return path;
            }
        }

        /// <summary>
        /// Gets JavaScript cache absolute path.
        /// </summary>
        public static string JsCacheAbsolutePath
        {
            get
            {
                return Path.Combine(HostingEnvironment.ApplicationPhysicalPath, CalipsoSection.JavaScriptManager.CachePath.Replace("~", string.Empty).Replace(@"\", string.Empty));
            }
        }

        /// <summary>
        /// Gets all components of current inversion of control container.
        /// </summary>
        public static ReadOnlyCollection<ComponentElement> Components
        {
            get
            {
                return new ReadOnlyCollection<ComponentElement>(CalipsoSection.Components.Cast<ComponentElement>().ToList());
            }
        }
    }
}